package com.changfen.config;

import feign.*;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.TimeUnit;


public class JacksonFeignConfiguration {
    @Bean
    Encoder JacksonEncoder(){
        return new JacksonEncoder();
    }

    @Bean
    ErrorDecoder errorDecoder(){
        return (s, response) -> {
            if(response.status() >=400 && response.status() <=499){
                return new Exception(response.reason());
            }

            return null;
        };
    }

    @Bean
    Retryer retryer(){
        return new Retryer.Default();
    }

    @Bean
    Request.Options options(){
        return new Request.Options(2, TimeUnit.SECONDS,5,TimeUnit.SECONDS,true);
    }

}
